[[...path]].page.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import React, { useMemo } from 'react';
  2. import { IUserHasId } from '@growi/core';
  3. import { model as mongooseModel } from 'mongoose';
  4. import {
  5. NextPage, GetServerSideProps, GetServerSidePropsContext,
  6. } from 'next';
  7. import { useTranslation } from 'next-i18next';
  8. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  9. import dynamic from 'next/dynamic';
  10. import { useRouter } from 'next/router';
  11. import { BasicLayout } from '~/components/Layout/BasicLayout';
  12. import { CrowiRequest } from '~/interfaces/crowi-request';
  13. import { ISidebarConfig } from '~/interfaces/sidebar-config';
  14. import { IUserUISettings } from '~/interfaces/user-ui-settings';
  15. import { UserUISettingsModel } from '~/server/models/user-ui-settings';
  16. import {
  17. useCurrentUser, useIsSearchPage,
  18. useIsSearchServiceConfigured, useIsSearchServiceReachable,
  19. useCsrfToken, useIsSearchScopeChildrenAsDefault,
  20. useRegistrationWhiteList, useShowPageLimitationXL,
  21. } from '~/stores/context';
  22. import {
  23. usePreferDrawerModeByUser, usePreferDrawerModeOnEditByUser, useSidebarCollapsed, useCurrentSidebarContents, useCurrentProductNavWidth,
  24. } from '~/stores/ui';
  25. import loggerFactory from '~/utils/logger';
  26. import {
  27. CommonProps, getNextI18NextConfig, getServerSideCommonProps, useCustomTitle,
  28. } from '../utils/commons';
  29. const logger = loggerFactory('growi:pages:me');
  30. type Props = CommonProps & {
  31. isSearchServiceConfigured: boolean,
  32. isSearchServiceReachable: boolean,
  33. isSearchScopeChildrenAsDefault: boolean,
  34. userUISettings?: IUserUISettings
  35. sidebarConfig: ISidebarConfig,
  36. showPageLimitationXL: number,
  37. // config
  38. registrationWhiteList: string[],
  39. };
  40. const PersonalSettings = dynamic(() => import('~/components/Me/PersonalSettings'), { ssr: false });
  41. // const MyDraftList = dynamic(() => import('~/components/MyDraftList/MyDraftList'), { ssr: false });
  42. const InAppNotificationPage = dynamic(
  43. () => import('~/components/InAppNotification/InAppNotificationPage').then(mod => mod.InAppNotificationPage), { ssr: false },
  44. );
  45. const MePage: NextPage<Props> = (props: Props) => {
  46. const router = useRouter();
  47. const { t } = useTranslation(['translation', 'commons']);
  48. const { path } = router.query;
  49. const pagePathKeys: string[] = Array.isArray(path) ? path : ['personal-settings'];
  50. const mePagesMap = useMemo(() => {
  51. return {
  52. 'personal-settings': {
  53. title: t('User Settings'),
  54. component: <PersonalSettings />,
  55. },
  56. // drafts: {
  57. // title: t('My Drafts'),
  58. // component: <MyDraftList />,
  59. // },
  60. 'all-in-app-notifications': {
  61. title: t('commons:in_app_notification.notification_list'),
  62. component: <InAppNotificationPage />,
  63. },
  64. };
  65. }, [t]);
  66. const getTargetPageToRender = (pagesMap, keys): {title: string, component: JSX.Element} => {
  67. return keys.reduce((pagesMap, key) => {
  68. return pagesMap[key];
  69. }, pagesMap);
  70. };
  71. const targetPage = getTargetPageToRender(mePagesMap, pagePathKeys);
  72. useIsSearchPage(false);
  73. useCurrentUser(props.currentUser ?? null);
  74. useRegistrationWhiteList(props.registrationWhiteList);
  75. useShowPageLimitationXL(props.showPageLimitationXL);
  76. // commons
  77. useCsrfToken(props.csrfToken);
  78. // // UserUISettings
  79. usePreferDrawerModeByUser(props.userUISettings?.preferDrawerModeByUser ?? props.sidebarConfig.isSidebarDrawerMode);
  80. usePreferDrawerModeOnEditByUser(props.userUISettings?.preferDrawerModeOnEditByUser);
  81. useSidebarCollapsed(props.userUISettings?.isSidebarCollapsed ?? props.sidebarConfig.isSidebarClosedAtDockMode);
  82. useCurrentSidebarContents(props.userUISettings?.currentSidebarContents);
  83. useCurrentProductNavWidth(props.userUISettings?.currentProductNavWidth);
  84. // // page
  85. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  86. useIsSearchServiceReachable(props.isSearchServiceReachable);
  87. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  88. return (
  89. <>
  90. <BasicLayout title={useCustomTitle(props, 'GROWI')}>
  91. <header className="py-3">
  92. <div className="container-fluid">
  93. <h1 className="title">{ targetPage.title }</h1>
  94. </div>
  95. </header>
  96. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  97. <div id="main" className='main'>
  98. <div id="content-main" className="content-main grw-container-convertible">
  99. {targetPage.component}
  100. </div>
  101. </div>
  102. </BasicLayout>
  103. </>
  104. );
  105. };
  106. async function injectUserUISettings(context: GetServerSidePropsContext, props: Props): Promise<void> {
  107. const req = context.req as CrowiRequest<IUserHasId & any>;
  108. const { user } = req;
  109. const UserUISettings = mongooseModel('UserUISettings') as UserUISettingsModel;
  110. const userUISettings = user == null ? null : await UserUISettings.findOne({ user: user._id }).exec();
  111. if (userUISettings != null) {
  112. props.userUISettings = userUISettings.toObject();
  113. }
  114. }
  115. async function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): Promise<void> {
  116. const req: CrowiRequest = context.req as CrowiRequest;
  117. const { crowi } = req;
  118. const {
  119. searchService,
  120. configManager,
  121. } = crowi;
  122. props.isSearchServiceConfigured = searchService.isConfigured;
  123. props.isSearchServiceReachable = searchService.isReachable;
  124. props.isSearchScopeChildrenAsDefault = configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  125. props.registrationWhiteList = configManager.getConfig('crowi', 'security:registrationWhiteList');
  126. props.showPageLimitationXL = crowi.configManager.getConfig('crowi', 'customize:showPageLimitationXL');
  127. props.sidebarConfig = {
  128. isSidebarDrawerMode: configManager.getConfig('crowi', 'customize:isSidebarDrawerMode'),
  129. isSidebarClosedAtDockMode: configManager.getConfig('crowi', 'customize:isSidebarClosedAtDockMode'),
  130. };
  131. }
  132. // /**
  133. // * for Server Side Translations
  134. // * @param context
  135. // * @param props
  136. // * @param namespacesRequired
  137. // */
  138. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  139. // preload all languages because of language lists in user setting
  140. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired, true);
  141. props._nextI18Next = nextI18NextConfig._nextI18Next;
  142. }
  143. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  144. const req = context.req as CrowiRequest<IUserHasId & any>;
  145. const { user, crowi } = req;
  146. const result = await getServerSideCommonProps(context);
  147. // check for presence
  148. // see: https://github.com/vercel/next.js/issues/19271#issuecomment-730006862
  149. if (!('props' in result)) {
  150. throw new Error('invalid getSSP result');
  151. }
  152. const props: Props = result.props as Props;
  153. if (user != null) {
  154. const User = crowi.model('User');
  155. const userData = await User.findById(req.user.id).populate({ path: 'imageAttachment', select: 'filePathProxied' });
  156. props.currentUser = userData.toObject();
  157. }
  158. await injectUserUISettings(context, props);
  159. await injectServerConfigurations(context, props);
  160. await injectNextI18NextConfigurations(context, props, ['translation', 'admin', 'commons']);
  161. return {
  162. props,
  163. };
  164. };
  165. export default MePage;